<template>
  <div id="view-user">
    <div class="tableControl m15">
      <el-button type="primary" icon="el-icon-plus" @click="addUser">添加用户</el-button>
      <el-button type="primary" icon="el-icon-minus" @click="batchDelUser" :disabled="!selection.length">批量删除</el-button>
    </div>
    <div class="userList m15">
      <table-field :config="config" ref="userList" @selection-change="selectionChange">
        <el-table-column type="selection" width="55" fixed="left" align="center">
        </el-table-column>
        <el-table-column label="用户名" prop="name"></el-table-column>
        <el-table-column label="性别" prop="sex">
          <template slot-scope="scope">
            <span v-if="scope.row.sex === '0'">未知</span>
            <span v-if="scope.row.sex === '1'">男</span>
            <span v-if="scope.row.sex === '2'">女</span>
          </template>
        </el-table-column>
        <el-table-column label="邮箱" prop="email" min-width="160px"></el-table-column>
        <el-table-column label="电话" prop="tel" min-width="120px"></el-table-column>
        <el-table-column label="地址" prop="address"></el-table-column>
        <el-table-column label="排序" prop="sort" align="center"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            {{ scope.row.status === 1 ? '正常' : '禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="超管" prop="isSuperAdmin" min-width="80px" align="center">
          <template slot-scope="scope">
            {{ scope.row.isSuperAdmin === 1 ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column label="管理员" prop="isAdmin" min-width="80px" align="center">
          <template slot-scope="scope">
            {{ scope.row.isAdmin === 1 ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column label="权限" prop="range" align="center"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="180px" align="center"></el-table-column>
        <el-table-column label="更新时间" prop="updateTime" min-width="180px" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" min-width="160px">
          <template slot-scope="scope">
            <el-tooltip content="编辑">
              <el-button class="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="重置密码">
              <el-button class="el-icon-refresh-right" @click="handleReset(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除">
              <el-button class="el-icon-delete" @click="handleDel(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </table-field>
    </div>
    <drawer-field :visible.sync="isShowDrawer" :title="drawerTitle" @confirm="confirm">
      <el-form :model="userData" :rules="rules" ref="userForm" label-width="auto" class="" :key="key">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="userData.name" @input="change($event)"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="!isEdit">
          <el-input v-model="userData.password" @input="change($event)" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="userData.sex">
            <el-radio label="1">男</el-radio>
            <el-radio label="2">女</el-radio>
            <el-radio label="0">未知</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userData.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="userData.tel"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="userData.address"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="userData.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="超管" prop="isSuperAdmin">
          <el-radio-group v-model="userData.isSuperAdmin">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="管理员" prop="isAdmin">
          <el-radio-group v-model="userData.isAdmin">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="userData.sort"></el-input>
        </el-form-item>
      </el-form>
    </drawer-field>
  </div>
</template>

<script>
import { addUserApi, updateUserApi, deleteUserApi, resetPasswordApi } from '@/api/user'
import TableField from '@/components/TableField'
import DrawerField from '@/components/DrawerField'
import { deepClone } from '@/utils/common'

export default {
  name: 'view-user',
  components: {
    [TableField.name]: TableField,
    [DrawerField.name]: DrawerField
  },
  data() {
    return {
      config: {
        url: '/user/list',
        rootProperty: 'list',
        data: {
        }
      },
      isShowDrawer: false,
      drawerTitle: '',
      isEdit: false,
      key: 0,
      selection: [],
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
        tel: [
          { required: true, message: '请输入电话', trigger: 'change' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'change' }
        ]
      },
      userData: {
        sex: '0',
        status: 1,
        isSuperAdmin: 0,
        isAdmin: 0
      }
    }
  },
  watch: {
    isShowDrawer(newVal) {
      newVal === false || this.key++
    }
  },
  methods: {
    getUserList(isReload) {
      this.$refs.userList.loadData(isReload)
    },
    addUser() {
      this.isShowDrawer = true
      this.isEdit = false
      this.drawerTitle = '添加员工'
      this.userDataInit()
    },
    batchDelUser() {
      this.handleDel(this.selection)
    },
    change() {
      this.$forceUpdate()
    },
    confirm() {
      if (!this.isEdit) {
        addUserApi(this.userData).then(res => {
          if (res.code === 1) {
            this.$message({
              type: 'success',
              message: '新建用户成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
          }
          this.getUserList()
          this.isShowDrawer = false
          this.userDataInit()
        })
          .catch(() => {
            this.isShowDrawer = false
            this.userDataInit()
          })
      } else {
        let params = this.userData
        delete params.password
        delete params.createUser
        delete params.createTime
        delete params.updateUser
        delete params.updateTime
        updateUserApi(params).then(res => {
          if (res.code === 1) {
            this.getUserList()
          }
          this.isShowDrawer = false
          this.userDataInit()
        })
          .catch(() => {
            this.isShowDrawer = false
            this.userDataInit()
          })
      }
    },
    handleEdit(row) {
      this.isShowDrawer = true
      this.isEdit = true
      this.drawerTitle = '编辑用户'
      this.userData = deepClone(row)
    },
    handleReset(row) {
      this.$confirm(`是否重置${row.name}的密码为123456?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        resetPasswordApi(row).then(res => {
          this.$message({
            type: 'success',
            message: '重置密码成功!'
          })
        })
          .catch(() => {
            this.$message({
              type: 'error',
              message: '重置密码失败!'
            })
          })
      })
      .catch(err => {
        console.log(err)
      })
    },
    handleDel(row) {
      let title = ''
      let params = null
      if (Array.isArray(row)) {
        params = row
        title += row.map(ele => ele.name).join(',')
      } else {
        params = [row]
        title = row.name
      }
      this.$confirm(`将删除${title}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUserApi(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除用户成功!'
          })
          this.getUserList()
        })
          .catch(() => {
            this.$message({
              type: 'error',
              message: '删除用户失败!'
            })
            this.getUserList()
          })
      }).catch(err => {
        console.log(err)
      })
    },
    selectionChange(selection) {
      this.selection = selection
    },
    userDataInit() {
      this.userData = {
        sex: '0',
        status: 1,
        isSuperAdmin: 0,
        isAdmin: 0
      }
    }
  },
  mounted() {
    this.getUserList(true)
  }
}
</script>

<style lang="scss" scoped>
#view-user {
  .tableControl {
    display: flex;
  }

  .userList {}
}
</style>
